'Populates the Outside Activities, Equity Interest or Family Association Forms with the OutsideEntity Name - AnswerFiller
Option Strict Off
imports System
imports PHSRAG.Insight21.Humans
imports PHSRAG.Insight21.BusinessObjects
imports PHSRAG.Insight21.FormsManagement
imports COI = PHSRAG.Insight21.COI
imports System.Collections

module Script
    sub Main()
		'Add the new QA to the COI
        dim outsideEntityAnswer as new Answer(cachePolicy)
		outsideEntityAnswer.QuestionnaireAnswerId = questAnswer.Id

		outsideEntityAnswer.Name = "OutsideEntityName"
		if (Convert.ToBoolean(coiParams.IsNewOutsideEntity)) then
			dim organization as new COI.COIOrganization(cachePolicy)
			organization.Name = Convert.ToString(coiParams.OutsideEntityName)
			outsideEntityAnswer.Value = Convert.ToString(coiParams.OutsideEntityName)
			questAnswer.Name = Convert.ToString(coiParams.OutsideEntityName)
			organization.AdmPersonId = Convert.ToInt32(coiParams.AdmPersonId)
			organization.Save(connectionString)
			questAnswer.ExternalId = organization.Id
			coiParams.OutsideEntitySelectedValue = organization.Id 'Used in the later part of the script
		else
			outsideEntityAnswer.Value = Convert.ToString(coiParams.OutsideEntityName)
			questAnswer.Name = Convert.ToString(coiParams.OutsideEntityName)
			questAnswer.ExternalId = Convert.ToInt32(coiParams.OutsideEntitySelectedValue)
		end if
		
		questAnswer.Answers.Add(outsideEntityAnswer)
		currentCOI.QuestionnaireAnswers.Add(questAnswer)
		currentCOI.Save(connectionString) 'takes care of the mapping record
		'End - Add the new QA to the COI
		
		'Update Individual Disclosure
		currentCOI.LoadQuestionnaireAnswers()
		dim questionnaireAnswers as ArrayList = currentCOI.QuestionnaireAnswers
		For Each questionnaireAnswer as QuestionnaireAnswer in questionnaireAnswers
			If (questionnaireAnswer.QuestionnaireCategory = PHSRAG.Insight21.FormsManagement.Constants.COICategory.InitialQuestionnaire) Then
				If not coiParams.MarkForDeletion Then
					dim parentAnswerList as ArrayList = questionnaireAnswer.GetAnswers("OutsideActivity")
					if parentAnswerList.Count > 0 Then
						dim parentAnswer as Answer = parentAnswerList(0)
						dim answer as new Answer(cachePolicy)
						answer.Name = "ListOutsideActivities"
						answer.Value = string.Format("{0}|{1}", coiParams.OutsideEntitySelectedValue.ToString(), coiParams.OutsideEntityName)
						answer.QuestionnaireAnswerId = questionnaireAnswer.Id 
						answer.ParentAnswerId = parentAnswer.Id
						questionnaireAnswer.Answers.Add(answer)
					End If
				Else
					dim answerValue as String = string.Format("{0}|{1}", coiParams.OutsideEntitySelectedValue.ToString(), coiParams.OutsideEntityName)
					dim answers as ArrayList = questionnaireAnswer.GetAnswers("ListOutsideActivities")
					For Each answer as Answer in answers
						If answer.Value = answerValue Then
							answer.MarkForDelete = true
							Exit For
						End If
					Next
					'The following check is to remove the parent answer that was answered "yes" for OutsideActivity question.
					answers = questionnaireAnswer.GetAnswers("ListOutsideActivities")
					dim hasNoOA as boolean = true
					For Each answer as Answer in answers
						If answer.MarkForDelete = false Then
						 hasNoOA = false
						 Exit For
						end if
					Next
					If hasNoOA = true Then
						dim parentAnswerList as ArrayList = questionnaireAnswer.GetAnswers("OutsideActivity")
						if parentAnswerList.Count > 0 Then
							dim parentAnswer as Answer = parentAnswerList(0)
							parentAnswer.Value = "N"
							parentAnswer.Save(connectionString)						
						End If
					End If 
				End If
				questionnaireAnswer.Save(connectionString)
				Exit For
			End If
		Next
		

    end sub
end module